package com.andong.controller;

import com.andong.annotations.AccessControl;
import com.andong.domain.entity.SubjectFour;
import com.andong.domain.pojo.PageResult;
import com.andong.domain.pojo.Result;
import com.andong.service.SubjectFourService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

import static com.andong.utils.SystemConstant.*;

/**
 * 科目四题库表(SubjectFour)表控制层
 *
 * @author makejava
 * @since 2024-01-06 20:45:09
 */
@RestController
@RequestMapping("/subjectFour")
@Slf4j
public class SubjectFourController {
    /**
     * 服务对象
     */
    @Resource
    private SubjectFourService subjectFourService;

    @PostMapping("/saveBatch")
    @AccessControl(roles = {ADMIN, SUPER_ADMIN}, permission = "资料管理")
    public Result saveBatch(MultipartFile file) throws IOException {
        return subjectFourService.addBatch(file);
    }

    @GetMapping("/{id}")
    @AccessControl(roles = {STUDENT, ADMIN, SUPER_ADMIN}, permission = "资料管理")
    public Result getById(@PathVariable Long id) {
        return subjectFourService.selectById(id);
    }

    @GetMapping()
    @AccessControl(roles = STUDENT)
    public Result getIds(Boolean isTest) {
        return subjectFourService.getIds(isTest);
    }

    @GetMapping("/list")
    @AccessControl(roles = {ADMIN, SUPER_ADMIN}, permission = "资料管理")
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name) {
        PageResult pageResult = subjectFourService.queryList(page, pageSize, name);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{ids}")
    @AccessControl(roles = {ADMIN, SUPER_ADMIN}, permission = "资料管理")
    public Result deleteByIds(@PathVariable List<Long> ids){
        log.info("批量删除操作, ids:{}",ids);
        return subjectFourService.deleteByIds(ids);
    }

    @PostMapping()
    @AccessControl(roles = {ADMIN, SUPER_ADMIN}, permission = "资料管理")
    public Result saveOrUpdate(@RequestBody SubjectFour subjectFour){
        return subjectFourService.saveOrModify(subjectFour);
    }
    @DeleteMapping()
    @AccessControl(roles = {ADMIN, SUPER_ADMIN}, permission = "资料管理")
    public Result deleteAll(){
        return subjectFourService.deleteAll();
    }
}

